/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataLayer.KnowledgeBase;

import ControllerLayer.ControlServices.Normalizer.INormalizer;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * Contains the information about a character type, all the images that represent it in the knowledgebase, along with its value and average.
 * @author csigabiga
 */
public class SingleCharInfos implements Serializable {

    public char ch;
    public Map<String, SerializableBufferedImage> imgList;
    public SerializableBufferedImage avg;
    private int chNr = 0;

    /**
     * The constructor.
     * @param c - The character for initialization.
     */
    public SingleCharInfos(char c) {
        ch = c;
        imgList = new HashMap<String, SerializableBufferedImage>();
        avg = new SerializableBufferedImage();
    }

    /**
     * Updates the average image by recalculating it, with the freshly iserted one.
     * @param md5ID - The image ID of the freshly inserted image.
     * @param normalizer - The normalizer component.
     * @throws NoSuchAlgorithmException
     */
    public void updateAverageImage(String md5ID, INormalizer normalizer) throws NoSuchAlgorithmException {
        if ((imgList.size() != 1) && (chNr != imgList.size())) {
            BufferedImage newBuffImg = normalizer.normalizeSingleChar(imgList.get(md5ID).getBufferedImage()); //update with this
            BufferedImage avgBuff = avg.getBufferedImage();

            ////System.out.println("averaging" + md5ID + "size: " + imgList.size() + "ch: " + ch);
            int w = avgBuff.getWidth();
            int h = avgBuff.getHeight();
            int s = imgList.size() - 1;
            int hp = newBuffImg.getHeight();
            int wp = newBuffImg.getWidth();

            //averaging the new image's colors and updating the average
            for (int i = 0; i < wp; ++i) {
                for (int j = 0; j < hp; ++j) {
                    Color colNew = new Color(newBuffImg.getRGB(i, j));
                    Color colAvg = new Color(avgBuff.getRGB(i, j));
                    double red = (colNew.getRed() * 1.0 / s) + colAvg.getRed();
                    double green = (colNew.getGreen() * 1.0 / s) + colAvg.getGreen();
                    double blue = (colNew.getBlue() * 1.0 / s) + +colAvg.getBlue();

                    red = ((red * s) * 1.0 / (s + 1)) / 255.0;
                    green = ((green * s) * 1.0 / (s + 1)) / 255.0;
                    blue = ((blue * s) * 1.0 / (s + 1)) / 255.0;

                    Color newColor = new Color((float) red, (float) green, (float) blue);
                    newBuffImg.setRGB(i, j, newColor.getRGB());

                }
            }
            avg = new SerializableBufferedImage(newBuffImg);
            chNr++;
        } else if (imgList.size() == 1) {
            avg = new SerializableBufferedImage(normalizer.normalizeSingleChar(imgList.get(md5ID).getBufferedImage()));
        }
    }
}
